package com.mo.star.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mo.star.common.BaseResponse;
import com.mo.star.common.ErrorCode;
import com.mo.star.common.ResultUtils;
import com.mo.star.exception.BusinessException;
import com.mo.star.model.UserRequest;
import com.mo.star.pojo.User;
import com.mo.star.service.impl.UsertableServiceImpl;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.mo.star.contant.UserConstant.USER_LOGIN_STATE;
@Log4j
@RestController

@RequestMapping("/user")
@Tag(name="用户")
@CrossOrigin(origins = {"http://localhost:5173"},allowCredentials = "true")
public class UserController {

    @Resource
    private UsertableServiceImpl usertableService;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     *
     * @param user
     * @return 成功返回用户id
     */
  /*  @PostMapping ("/register")
    @ResponseBody
    public BaseResponse<String> userRegister(@RequestBody UserRequest user){
        if(user == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        if(StringUtils.isAnyBlank(user.getPwd(),user.getUsername()))
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        String re = userService.doRegister(user.getUsername(), user.getPwd());
        return ResultUtils.success(re);
    }*/
    @PostMapping ("/login")
    @ResponseBody
    public BaseResponse<User> userLogin(@RequestBody UserRequest user, HttpServletRequest request){
        if(StringUtils.isAnyBlank(user.getPwd(),user.getUsername()))
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        User user1 =usertableService.userLogin(user.getUsername(),user.getPwd(),request);
        return ResultUtils.success(user1);
    }
    @PostMapping("/SeUsersTags")
    public BaseResponse <List<User>> searchUsersByTags(@RequestBody(required = false) List<String> tagList){
        if(CollectionUtils.isEmpty(tagList))
            throw new BusinessException(ErrorCode.NULL_ERROR);
        List<User> userList = usertableService.searchUsersByTags(tagList);
        return ResultUtils.success(userList);
    }
    @GetMapping("/current")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User currentUser = (User) userObj;
        if (currentUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        String userId = currentUser.getId();
        User user = usertableService.getById(userId);
        User safetyUser = usertableService.getSafetyUser(user);
        return ResultUtils.success(safetyUser);
    }
    @PostMapping("/update")
    public BaseResponse<User> update(@RequestBody User user,HttpServletRequest request ){
        if(user==null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
         User userLogin=usertableService.getLoginUser(request);

       User newUser= usertableService.updateUser(user,userLogin);
        return ResultUtils.success(newUser);
    }

    /**
     * 根据不同用户推荐不同内容
     * @param pageSize
     * @param pageNum
     * @param request
     * @return
     */
    @GetMapping("/recommend")
    public BaseResponse<Page<User>> recommendUsers(long pageSize,long pageNum,HttpServletRequest request){
        //获得已登录用户信息
        User loginUser = usertableService.getLoginUser(request);
       Page<User> userPage= usertableService.doRedis(loginUser);
       return ResultUtils.success(userPage);
    }
    @GetMapping("/match")
    public BaseResponse<List<User>> matchUsers (@RequestParam("num") int num ,HttpServletRequest request){
        if(num <= 0 || num>21)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        User user = usertableService.getLoginUser(request);
        return ResultUtils.success(usertableService.match(num,user));
    }
    @RequestMapping("/TestConnect")
    public BaseResponse<String> TestConnect(){
        return ResultUtils.success("已建立连接");
    }
}
